﻿using FFmpeg.AutoGen;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FFmpegWinui3.MediaFolder
{
    public unsafe class MediaFrame
    {
        public AVFrame* frame { get; }
        public TimeSpan StartTime { get; }
        public TimeSpan Duration { get; }
        public TimeSpan EndTime { get; }
        public MediaFrame(AVFrame* frame, AVRational streamTimeBase)
        {
            StartTime = frame->pts == long.MinValue ?
                TimeSpan.FromTicks(0) :
                TimeSpan.FromTicks(frame->pts.ToTimeSpan(streamTimeBase).Ticks);
            // 计算音频帧持续时间
            if (frame->pkt_duration != 0)
                Duration = frame->pkt_duration.ToTimeSpan(streamTimeBase);
            else
                Duration = TimeSpan.FromTicks((long)Math.Round(TimeSpan.TicksPerMillisecond * 1000d * frame->nb_samples / frame->sample_rate, 0));
            EndTime = TimeSpan.FromTicks(StartTime.Ticks + Duration.Ticks);

            this.frame = frame;
        }

        public void DisponseFrame()
        {
            var temp = frame;
            ffmpeg.av_frame_free(&temp);
        }
    }
}
